//
//  User.m
//  CarLifes
//
//  Created by yu on 2017/8/4.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "User.h"

#define USER_USER_ID @"userId"  // 用户Id
#define USER_DEPT_ID @"deptId" // 部门Id
#define user_child_dept_id @"deptId" // 部门Id
#define user_parent_dept_id @"parentDeptId" // 上级部门Id
#define USER_NICK_NAME @"nickName" // 用户昵称
#define USER_DEPT_NAME @"deptName" // 用户名称
#define USER_MOBILE @"mobile" // 用户电话
#define USER_EMAIL @"email" // 用户邮箱
#define USER_AVATARURL @"avatarUrl" // 用户头像

#define USER_CITY @"city" // 用户所在城市(用于请求实时天气)

// 账户登录帐号
#define USER_ACCOUNT @"account"
#define USER_ACCOUNT_PASSWORD @"password"

// 车架号登录帐号
#define USER_VIN_ACCOUNT @"vinAccount"
#define USER_VIN_PASSWORD @"vinPassword"

#define USER_SAVE_VALUE @"saveValue"

#define USER_SEARCH_TEXT @"searchText"

/*定位位置相关*/
///格式化地址
#define user_loc_formattedAddress @"formattedAddress"
/////省/直辖市
#define user_loc_province @"province"
/////市
#define user_loc_city @"city"
/////区
#define user_loc_district @"district"
/////街道名称
#define user_loc_street @"street"
/////纬度
#define user_loc_latitude @"latitude"
/////经度
#define user_loc_longitude @"longitude"


// 记录登录方式
#define user_login_type @"loginType"

#define current_version @"version"


/*工单登录*/
#define user_workorder_account @"workorderAccount"
// 账户保存状态
#define user_workorder_account_save_status @"workorderAccountStatus"

/*家访*/
#define user_workorder_visitor_name @"visitorName"
#define user_workorder_visitor_mobile @"visitorMobile"

@implementation User

- (void)logout {}

+ (User *)sharedUser {
    static User *sharedUserInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedUserInstance = [[User alloc] init];
    });
    return sharedUserInstance;
}

#pragma mark Property Setter And Getter

- (NSString *)verson
{
    return [self.userDefaults objectForKey:current_version];
}

- (void)setVerson:(NSString *)verson
{
    [self.userDefaults setObject:verson forKey:current_version];
}

- (NSString *)userId {
    return [self.userDefaults objectForKey:USER_USER_ID];
}

- (void)setUserId:(NSString *)userId {
    [self.userDefaults setObject:userId forKey:USER_USER_ID];
}

- (NSString *)nickName {
    return [self.userDefaults objectForKey:USER_NICK_NAME];
}

- (void)setNickName:(NSString *)nickName {
    [self.userDefaults setObject:nickName forKey:USER_NICK_NAME];
}

- (NSString *)deptName {
    return [self.userDefaults objectForKey:USER_DEPT_NAME];
}

- (void)setDeptName:(NSString *)deptName {
    [self.userDefaults setObject:deptName forKey:USER_DEPT_NAME];
}

- (NSString *)avatar {
    return [self.userDefaults objectForKey:USER_AVATARURL];
}

- (void)setAvatar:(NSString *)avatar {
    [self.userDefaults setObject:avatar forKey:USER_AVATARURL];
}


- (NSString *)mobile {
    return [self.userDefaults objectForKey:USER_MOBILE];
}

- (void)setMobile:(NSString *)mobile {
    [self.userDefaults setObject:mobile forKey:USER_MOBILE];
}


- (NSString *)email {
    return [self.userDefaults objectForKey:USER_EMAIL];
}

- (void)setEmail:(NSString *)email {
    [self.userDefaults setObject:email forKey:USER_EMAIL];
}


#pragma mark - account & password -
- (NSString *)account {
    return [self.userDefaults objectForKey:USER_ACCOUNT];
}

- (void)setAccount:(NSString *)account {
    [self.userDefaults setObject:account forKey:USER_ACCOUNT];
}

- (NSString *)password {
    return [self.userDefaults objectForKey:USER_ACCOUNT_PASSWORD];
}

- (void)setPassword:(NSString *)password {
    [self.userDefaults setObject:password forKey:USER_ACCOUNT_PASSWORD];
}

#pragma mark - vin account & vin password -

- (void)setLoginType:(LoginType)loginType{
    if (loginType == LoginTypeAccount) {
        [self.userDefaults setObject:@"LoginTypeAccount" forKey:user_login_type];
    } else{
        [self.userDefaults setObject:@"LoginTypeVin" forKey:user_login_type];
    }
}
- (LoginType)loginType{
    NSString *type = [self.userDefaults objectForKey:user_login_type];
    if ([type isEqualToString:@"LoginTypeAccount"]) {
        return LoginTypeAccount;
    } else if ([type isEqualToString:@"LoginTypeVin"]){
        return LoginTypeVin;
    } else {
        return LoginTypeAccount;
    }
}


- (NSString *)vinAccount {
    return [self.userDefaults objectForKey:USER_VIN_ACCOUNT];
}

- (void)setVinAccount:(NSString *)vinAccount {
    [self.userDefaults setObject:vinAccount forKey:USER_VIN_ACCOUNT];
}


- (NSString *)vinPassword {
    return [self.userDefaults objectForKey:USER_VIN_PASSWORD];
}

- (void)setVinPassword:(NSString *)vinPassword {
    [self.userDefaults setObject:vinPassword forKey:USER_VIN_PASSWORD];
}

- (BOOL)isSavePsd {
    return [self.saveValue isEqual:@(1)] ? YES : NO;
}

- (NSNumber *)saveValue {
    return [self.userDefaults objectForKey:USER_SAVE_VALUE];
}

- (void)setSaveValue:(NSNumber *)saveValue {
    [self.userDefaults setObject:saveValue forKey:USER_SAVE_VALUE];
}

#pragma mark - workorder account -
- (void)setWorkorderAccount:(NSString *)workorderAccount {
    [self.userDefaults setObject:workorderAccount forKey:user_workorder_account];
}

- (NSString *)workorderAccount {
    return [self.userDefaults objectForKey:user_workorder_account];
}

- (void)setSaveWorkorderAccountStatus:(NSNumber *)saveWorkorderAccountStatus {
    [self.userDefaults setObject:saveWorkorderAccountStatus forKey:user_workorder_account_save_status];
}

- (NSNumber *)saveWorkorderAccountStatus {
    return [self.userDefaults objectForKey:user_workorder_account_save_status];
}

- (BOOL)isSaveWorkorderAccount {
    return [self.saveWorkorderAccountStatus isEqual:@(1)] ? YES:NO;
}


- (void)setSearchText:(NSString *)searchText {
    [self.userDefaults setObject:searchText forKey:USER_SEARCH_TEXT];
}

- (NSString *)searchText {
    return [self.userDefaults objectForKey:USER_SEARCH_TEXT];
}

#pragma mark - Workorder setter && getter

- (void)setVisitorName:(NSString *)visitorName {
    [self.userDefaults setObject:visitorName forKey:user_workorder_visitor_name];
}

- (NSString *)visitorName {
    return [self.userDefaults objectForKey:user_workorder_visitor_name];
}

- (void)setVisitorMobile:(NSString *)visitorMobile {
    [self.userDefaults setObject:visitorMobile forKey:user_workorder_visitor_mobile];
}

- (NSString *)visitorMobile {
    return [self.userDefaults objectForKey:user_workorder_visitor_mobile];
}



- (DeptModel *)deptModel {
    if (_deptModel == nil) {
        _deptModel = [[DeptModel alloc] init];
    }
    return _deptModel;
}


///格式化地址
//@property (nonatomic, copy) NSString *formattedAddress;
/////省/直辖市
//@property (nonatomic, copy) NSString *province;
/////市
//@property (nonatomic, copy) NSString *city;
/////区
//@property (nonatomic, copy) NSString *district;
/////街道名称
//@property (nonatomic, copy) NSString *street;

- (void)setFormattedAddress:(NSString *)formattedAddress {
    [self.userDefaults setObject:formattedAddress forKey:user_loc_formattedAddress];
}
- (NSString *)formattedAddress {
    return [self.userDefaults objectForKey:user_loc_formattedAddress];
}

- (void)setProvince:(NSString *)province {
    [self.userDefaults setObject:province forKey:user_loc_province];
}
- (NSString *)province {
    return [self.userDefaults objectForKey:user_loc_province];
}

- (void)setCity:(NSString *)city {
    [self.userDefaults setObject:city forKey:user_loc_city];
}
- (NSString *)city {
    return [self.userDefaults objectForKey:user_loc_city];
}

- (void)setDistrict:(NSString *)district {
    [self.userDefaults setObject:district forKey:user_loc_district];
}
- (NSString *)district {
    return [self.userDefaults objectForKey:user_loc_district];
}

- (void)setStreet:(NSString *)street {
    [self.userDefaults setObject:street forKey:user_loc_street];
}
- (NSString *)street {
    return [self.userDefaults objectForKey:user_loc_street];
}

- (void)setLatitude:(NSString *)latitude {
    [self.userDefaults setObject:latitude forKey:user_loc_latitude];
}
- (NSString *)latitude {
    return [self.userDefaults objectForKey:user_loc_latitude];
}

- (void)setLongitude:(NSString *)longitude {
    [self.userDefaults setObject:longitude forKey:user_loc_longitude];
}
- (NSString *)longitude {
    return [self.userDefaults objectForKey:user_loc_longitude];
}



//- (void)updateUserLoctionInfo:(AMapLocationReGeocode *)info {
//    self.formattedAddress = info.formattedAddress;
//    self.province = info.province;
//    self.city = info.city;
//    self.district = info.district;
//    self.street = info.street;
////    self.latitude =
//}

- (void)updateUserLoction:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    self.formattedAddress = reGeocode.formattedAddress;
    self.province = reGeocode.province;
    self.city = reGeocode.city;
    self.district = reGeocode.district;
    self.street = reGeocode.street;
    self.latitude = NSStringFormat(@"%f",location.coordinate.latitude);
    self.longitude = NSStringFormat(@"%f",location.coordinate.longitude);
}


@end



@implementation DeptModel

- (NSString *)deptId {
    return [self.userDefaults objectForKey:USER_DEPT_ID];
}
- (void)setDeptId:(NSString *)deptId {
    [self.userDefaults setObject:deptId forKey:USER_DEPT_ID];
}

- (void)setChildDeptId:(NSString *)childDeptId {
    [self.userDefaults setObject:childDeptId forKey:user_child_dept_id];
}
- (NSString *)childDeptId {
    return [self.userDefaults objectForKey:user_child_dept_id];
}

- (void)setParentDeptId:(NSString *)parentDeptId {
    [self.userDefaults setObject:parentDeptId forKey:user_parent_dept_id];
}
- (NSString *)parentDeptId
{
    return [self.userDefaults objectForKey:user_parent_dept_id];
}

@end

